package abcy.cloud.syscoreserver.services.impls;

import abcy.cloud.blogcommon.enums.SignEnum;
import abcy.cloud.blogcommon.utils.UUIDUtil;
import abcy.cloud.syscoreserver.dto.req.SysMenuAddReqDto;
import abcy.cloud.syscoreserver.dto.req.SysMenuSelectReqDto;
import abcy.cloud.syscoreserver.dto.resp.SysMenuSelectRespDto;
import abcy.cloud.syscoreserver.entities.SysMenu;
import abcy.cloud.syscoreserver.mappers.SysMenuMapper;
import abcy.cloud.syscoreserver.services.SysMenuService;
import abcy.cloud.syscoreserver.utils.CodeUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.NumberFormat;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Override
    public PageInfo<SysMenuSelectRespDto> pageInfo(SysMenuSelectReqDto sysMenuSelectReqDto) {
        PageHelper.startPage(sysMenuSelectReqDto.getPageIndex(), sysMenuSelectReqDto.getPageSize());
        List<SysMenuSelectRespDto> menuSelectRespDtos = sysMenuMapper.getMenusForPage(sysMenuSelectReqDto);
        PageInfo<SysMenuSelectRespDto> pageInfo = new PageInfo<>(menuSelectRespDtos);
        return pageInfo;
    }

    @Override
    public void addMenu(SysMenuAddReqDto sysMenuAddReqDto) {
        SysMenu sysMenu = new SysMenu();
        String parentId = sysMenuAddReqDto.getParentId();
        BeanUtils.copyProperties(sysMenuAddReqDto,sysMenu);
        SysMenuSelectRespDto sysMenuSelectResp = sysMenuMapper.getParentMenuInfo(sysMenuAddReqDto.getParentId());
        String code = CodeUtil.codeGenerator(SignEnum.WHITE.getValue(), sysMenuMapper.getMaxMenusCode());

        if (Objects.nonNull(sysMenuSelectResp)) {
            List<String> parentIds = Arrays.asList(sysMenuSelectResp.getParentIds().split(","));
            List<String> pslist = new ArrayList<>();
            for(String val: parentIds) {
                pslist.add(val);
            }
            pslist.add(parentId);
            String pids = pslist.stream()
                    .map(n -> String.valueOf(n))
                    .collect(Collectors.joining(","));
            sysMenu.setParentIds(pids);
            sysMenu.setLevel(sysMenuSelectResp.getLevel()+1);
            if(sysMenuSelectResp.getLevel()+1 == 2) {
                sysMenu.setLevelDesc("二级菜单");
            } else if(sysMenuSelectResp.getLevel()+1 == 3) {
                sysMenu.setLevelDesc("三级菜单");
            }
        } else {
            sysMenu.setParentIds("0");
            sysMenu.setLevel(1);
            sysMenu.setLevelDesc("一级菜单");
        }
        sysMenu.setCode(code);
        sysMenu.setIsDel(sysMenuAddReqDto.getIsDel());
        sysMenu.setType(sysMenuAddReqDto.getType());
        String id = UUIDUtil.UUID();
        Date date = new Date();
        sysMenu.setId(id);
        sysMenu.setCreateTime(date);
        sysMenu.setUpdateTime(date);
        sysMenuMapper.insert(sysMenu);
    }
}
